<style>
    body{
        background-color: #F9F9F9;
    }
    td>label{
        margin-right: 10px;
        float: right;
    }
    html,body{background-color: #F9F9F9;}
    .mainPart{width:600px;margin:0 auto;padding:20px;}
    .ztree li{margin: 15px 0;}
    .ztree li span{line-height: 20px;}
    .ztree li a.curSelectedNode{height: 20px;}
    .ztree .level0 > span{font-size: 16px;font-weight: bold;}
    .ztree .level1 span{font-size: 16px;}
    .ztree li span.publicType{font-size: 14px;padding: 0 4px;border-radius: 2px;margin-right:10px;}
    .pink{background-color: #ffaaa3 !important;color: #fff !important;}
    .yellow{background-color: #ffb26a !important;color: #fff !important;}
    .green{background-color: #06c8a9 !important;color: #fff !important;}
    /*.ztree li span.button.noline_open{background: url(assets/img/icon_arrow.png) no-repeat center;}*/
    /*.ztree li span.button.noline_close{background: url(assets/img/icon_arrow.png) no-repeat center;transform: rotate(-90deg);-o-transform: rotate(-90deg);-webkit-transform: rotate(-90deg);-moz-transform: rotate(-90deg);}*/
    .ztree li span.button.switch{margin: 0 10px;}
    #treeDemo .badge,.badge{float:right;font-size: 14px;color:black;font-weight: normal;}
    .ztree li a.curSelectedNode {height: 20px;line-height: 20px;}
    .ztree li a.curSelectedNode .node_name{color:#3399fe;}
    .ztree li a.curSelectedNode {padding-top: 0px;background-color: #fff;color: black;height: 16px;border: none;opacity: 1;}
    .ztree li a:hover {text-decoration: none;}
    #title {line-height: 25px;}
    #title .bookName{font-weight: bold;font-size: 20px;}
</style>
<link rel="stylesheet" type="text/css" href="../manage/CA102001/assets/zTree/zTreeStyle/zTreeStyle.css"/>
<script>
    var nodes = {{ treeData }};
    var id = {% if id is defined %} {{ id }} {% else %} null {% endif %};
    var type = {{ type }};
    var isCheck = null;
</script>
<div class="mainPart" >
    <div id="title"><span class="bookName">{{ treeName }}</span></div>
    <div id="treeDemo" class="ztree"></div>
</div>
<script src="../manage/CA102001/assets/jquery-1.7.2.min.js"></script>
<script src="../manage/CA102001/assets/zTree/jquery.ztree.all.min.js"></script>
{#<script src="../assets/layui/layui.js"></script>#}
{#<script src="../common/Common.js"></script>#}
<script>
    (function ($) {
        $.frontPage = function () {
            return {
                init: function () {
                    this.listener();
                    $.frontPage.initZtree($("#treeDemo"), nodes);
                    // if (type == 2) {
                    //     $('#plan_cate_log_form', window.top.document).find('[id="plan_cate_log_confirm"]').attr('disabled', true);
                    // } else if (type == 1) {
                    //     $('#cate_log_form', window.top.document).find('[id="cate_log_confirm"]').attr('disabled', true);
                    // }

                },
                listener: function () {
                    //添加按钮
                    if (type == 2) {
                        $('#plan_cate_log_form', window.top.document).find('[id="plan_cate_log_confirm"]').click(function () {
                            $.frontPage.addFormData();
                        });
                    } else if (type == 1 || type == 6) {
                        $('#cate_log_form', window.top.document).find('[id="cate_log_confirm"]').click(function () {
                            $.frontPage.addFormData();
                        });
                    } else if (type == 3 || type == 4) {
                        $('#schedule_cate_log_form', window.top.document).find('[id="schedule_cate_log_confirm"]').click(function () {
                            $.frontPage.addFormData();
                        });
                    }

                },
                initZtree: function (othis, data) {
                    var setting = {
                        view: {
                            showLine: false,
                            showIcon: false,
                            addDiyDom: $.frontPage.addDiyDom
                        },
                        data: {
                            simpleData: {
                                enable: true
                            }
                        },
                        check: {
                            enable: true,
                            chkStyle: "checkbox",
                            chkboxType: { "Y": "ps", "N": "ps" },
                            autoCheckTrigger: true
                        },
                        callback:{
                            //选中时其他章节不可选
                            onCheck: $.frontPage.zTreeOnCheck
                        }
                    };
                    $.fn.zTree.init($("#treeDemo"), setting, data);
                },
                addDiyDom:function (treeId, treeNode) {
                    var aObj = $("#" + treeNode.tId );
                    // if(treeNode.count){
                    //     aObj.append("<span class='badge'>"+treeNode.count+"</span>");
                    // }
                    if(treeNode.children === undefined){//如果是子节点 则把状态加入
                        var publicTypeHtml='',publicTypeClass='';
                        switch (treeNode.publishStatus){
                            case 1:
                                publicTypeHtml="录入中";
                                publicTypeClass="pink";
                                break;
                            case 2:
                                publicTypeHtml="待校对";
                                publicTypeClass="yellow";
                                break;
                            case 3:
                                publicTypeHtml="校对中";
                                publicTypeClass="yellow";
                                break;
                            case 4:
                                publicTypeHtml="已校对";
                                publicTypeClass="green";
                                break;
                            case 5:
                                publicTypeHtml="已发布";
                                publicTypeClass="green";
                                break;
                            default:
                                publicTypeHtml="待录入";
                                publicTypeClass="pink";
                                break;
                        }
                        aObj.find(">a").prepend("<span class='publicType "+publicTypeClass+"'>"+publicTypeHtml+"</span>")
                    }
                },
                zTreeOnCheck: function(event, treeId, treeNode) {
                    // alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);
                    if (treeNode.parentTId === null) {//顶层
                        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                        // var nodes = treeObj.getNodes();//所有根节点
                        if (treeNode.checked === true) {//选中
                            if (isCheck) {//存在已选中
                                if (treeNode !== isCheck && treeNode.isParent) {
                                    window.top.xhTop.showError('已选择，不可多选');
                                    treeObj.checkNode(treeNode,false,true);
                                }
                            } else {//不存在已选中
                                if (treeNode.isParent) {
                                    isCheck = treeNode;
                                }
                            }
                        } else {//取消选中
                            if (isCheck === treeNode) {
                                isCheck = null;
                            }
                        }


                        // for (var i=0, l=nodes.length; i < l; i++) {
                        //     if (nodes[i] !== treeNode) {//其他章节不可选
                        //
                        //         if (treeNode.checked === true) {//选中时禁用
                        //             console.log(nodes[i], treeNode);
                        //             if (isCheck) {
                        //                 alert('不能选');return false;
                        //                 treeObj.setChkDisabled(nodes[i], true, true, true);
                        //             }
                        //
                        //             // if (type == 2) {
                        //             //     $('#plan_cate_log_form', window.top.document).find('[id="plan_cate_log_confirm"]').attr('disabled', false);
                        //             // } else if (type == 1) {
                        //             //     $('#cate_log_form', window.top.document).find('[id="cate_log_confirm"]').attr('disabled', false);
                        //             // }
                        //         } else {
                        //             isCheck = false;
                        //             // if (type == 2) {
                        //             //     $('#plan_cate_log_form', window.top.document).find('[id="plan_cate_log_confirm"]').attr('disabled', true);
                        //             // } else if (type == 1) {
                        //             //     $('#cate_log_form', window.top.document).find('[id="cate_log_confirm"]').attr('disabled', true);
                        //             // }
                        //             treeObj.setChkDisabled(nodes[i], false, true, true);
                        //         }
                        //     } else {
                        //         if (treeNode.checked === true) {//选中
                        //             isCheck = treeNode;
                        //
                        //             console.log(nodes[i], treeNode);
                        //             if (isCheck) {
                        //                 alert('不能选');return false;
                        //                 treeObj.setChkDisabled(nodes[i], true, true, true);
                        //             }
                        //         } else {//取消选中
                        //             isCheck = false;
                        //             treeObj.setChkDisabled(nodes[i], false, true, true);
                        //         }
                        //         isCheck = treeNode;
                        //         return false;
                        //     }
                        // }
                    }
                },
                addFormData: function () {//填充数据
                    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                    var nodes = treeObj.getCheckedNodes();
                    var checkNodes = [];
                    var checkChildArr = [];
                    var haveParent = false;
                    $.each(nodes, function (key, value) {
                        if (value.level === 0) {
                            if (value.check_Child_State === 2) {////全部节点选中只显示最高层
                                checkNodes.push(value.id);
                                haveParent = true;
                                checkChildArr = [];
                            }
                            if (value.check_Child_State === -1) {//没有子节点
                                checkNodes.push(value.id);
                            }
                        } else {
                            if (!haveParent) {//最高层不是全选
                                checkChildArr.push(value.id);
                            }
                        }
                        // if (value.level === 0 && value.check_Child_State === 2) {//全部节点选中只显示最高层
                        //
                        //     checkNodes.push(value.id);
                        //     return false;
                        // }
                        // if (value.level !== 0) {
                        //     checkNodes.push(value.id);
                        // }
                    });
                    $.merge(checkNodes,checkChildArr);//合并
                    if (type == 2) {
                        $('#plan_form', window.top.document).find("iframe")[0].contentWindow.obj.funObj().addCateLog(checkNodes, id);
                        $('#plan_cate_log_form', window.top.document).find('[id="bt_modal_close"]').click();
                    } else if (type == 1 || type == 6) {
                        $('#apply_form', window.top.document).find("iframe")[0].contentWindow.obj.funObj().addCateLog(checkNodes);
                        $('#cate_log_form', window.top.document).find('[id="bt_modal_close"]').click();
                    } else if (type == 3 || type == 4) {
                        $('#schedule_form', window.top.document).find("iframe")[0].contentWindow.obj.funObj().addCateLog(checkNodes, id);
                        $('#schedule_cate_log_form', window.top.document).find('[id="bt_modal_close"]').click();
                    }

                }
            }
        }();
        $.frontPage.init();
    })(jQuery);
</script>